// pages/label/index/index.js
const {
    baseUrl,
    isHaveToken,
    isHaveLogin
} = require("../../../utils/util")

// const {getUserInfo}=require("../../../utils/api")
// const {
//   getLaberData
// }
// require('../../../utils/api').getLaberData
Page({
    /**
     * 页面的初始数据
     */
    data: {
        finalList: [],//推荐
        ShuDong: [],//树洞
        ShuZhai:[],//书斋
        BangBang: [],//帮帮
        FindLost: [],//失物招领
        current: 1,
        limit: 10,
        total: 0,
        shwoAdd: false,
        tpye: "推荐",
        stickyProps: {
            zIndex: 2,
        },
    },
    gotoAdd(e) {
        // console.log(e)
        wx.navigateTo({
            url: '../addLabel/addLabel',
        })
    },
    onTabsChange(event) {
        this.setData({
            tpye: event.detail.value,
            current: 1
        })
        if (event.detail.value == "推荐") {
            // console.log("帮帮")
            this.getLabelList(1, 10)
        }
        console.log(`Change tab, tab-panel value is ${event.detail.value}.`);
        if (event.detail.value == "帮帮") {
            // console.log("帮帮")
            this.getLabelListByType("帮帮", 1, 10)
        }
        if (event.detail.value == "树洞") {
            // console.log("帮帮")
            this.getLabelListByType("树洞", 1, 10)
        }
        if (event.detail.value == "失物招领") {
            // console.log("帮帮")
            this.getLabelListByType("失物招领", 1, 10)
        }
        if (event.detail.value == "书摘") {
            // console.log("帮帮")
            this.getLabelListByType("书摘", 1, 10)
        }
    },

    onTabsClick(event) {
        this.setData({
            tpye: event.detail.value,
            current: 1
        })
        console.log(`Click tab, tab-panel value is ${event.detail.value}.`);
    },

    onStickyScroll(event) {
        console.log(event.detail);
    },
    handleItemChange(e) {
        console.log(e)
    },

    getLabelList(current, limit) {
        var that = this
        // getLaberData()
        wx.request({
            url: baseUrl + `/label/pageLabel/${current}/${limit}`,
            success(res) {
                console.log("index for lober", res)
                that.setData({
                    finalList: [...that.data.finalList, ...res.data.data.finalList],
                    total: res.data.data.total,
                })
            }
        })
    },
    getLabelListByType(type, current, limit) {
        var that = this
        // getLaberData()
        // /label/type/帮帮/1/10
        if (type == "帮帮") {
            wx.request({
                url: baseUrl + `/label/type/%E5%B8%AE%E5%B8%AE/${current}/${limit}`,
                success(res) {
                    console.log("getLabelListByType", res)
                    that.setData({
                        BangBang: [...that.data.BangBang, ...res.data.data.finalTypeList],
                        total: res.data.data.total,
                    })
                }
            })
        } else if (type == "树洞") {
            wx.request({
                url: baseUrl + `/label/type/%E6%A0%91%E6%B4%9E/${current}/${limit}`,
                success(res) {
                    console.log("getLabelListByType", res)
                    that.setData({
                        ShuDong: [...that.data.ShuDong, ...res.data.data.finalTypeList],
                        total: res.data.data.total,
                    })
                }
            })
        } else if (type == "失物招领") {
            wx.request({
                url: baseUrl + `/label/type/%E5%A4%B1%E7%89%A9%E6%8B%9B%E9%A2%86/${current}/${limit}`,
                success(res) {
                    console.log("getLabelListByType", res)
                    that.setData({
                        FindLost: [...that.data.FindLost, ...res.data.data.finalTypeList],
                        total: res.data.data.total,
                    })
                }
            })
        }else if (type == "书摘") {
            wx.request({
                url: baseUrl + `/label/type/%E4%B9%A6%E6%91%98/${current}/${limit}`,
                success(res) {
                    console.log("getLabelListByType", res)
                    that.setData({
                        ShuZhai: [...that.data.ShuZhai, ...res.data.data.finalTypeList],
                        total: res.data.data.total,
                    })
                }
            })
        }
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        if (isHaveLogin()) {
            this.setData({
                shwoAdd: true
            })
        }
        this.getLabelList(this.data.current, this.data.limit)
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {
        // console.log(this.data.tpye)
        if (this.data.tpye == "推荐") {
            // console.log("推荐下拉")
            this.data.finalList = []
            this.data.current = 1
            this.getLabelList(this.data.current, this.data.limit)
        }
        if (this.data.tpye == "树洞") {
            // console.log("树洞下拉",)
            this.data.ShuDong = []
            this.data.current = 1
            this.getLabelListByType("树洞", this.data.current, this.data.limit)
        }
        if (this.data.tpye == "帮帮") {
            // console.log("帮帮下拉",)
            this.data.BangBang = []
            this.data.current = 1
            this.getLabelListByType("帮帮", this.data.current, this.data.limit)
        }
        if (this.data.tpye == "失物招领") {
            // console.log("失物招领下拉",)
            this.data.FindLost = []
            this.data.current = 1
            this.getLabelListByType("失物招领", this.data.current, this.data.limit)
        }
        if (this.data.tpye == "书摘") {
            // console.log("书摘下拉",)
            this.data.ShuZhai = []
            this.data.current = 1
            this.getLabelListByType("书摘", this.data.current, this.data.limit)
        }
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom(e) {
        // console.log("上拉")
        wx.showLoading({
            title: 'loading',
            text: "加载中"
        })
        // console.log(this.data.current)
        this.data.current++
        // console.log(this.data.current)
        if (this.data.tpye == "推荐") {
            //推荐上拉
            console.log("推荐上拉")
            if ((this.data.current * this.data.limit) <= (this.data.total + 10)) {
                wx.hideLoading({
                    success: (res) => {
                        this.getLabelList(this.data.current, this.data.limit)
                    },
                })
            } else {
                // console.log("dayu")
                wx.showToast({
                    title: '没有更多了',
                })
            }
        } else if (this.data.tpye == "帮帮") {
            if ((this.data.current * this.data.limit) < (this.data.total + 10)) {
                wx.hideLoading({
                    success: (res) => {
                        this.getLabelListByType("帮帮", this.data.current, this.data.limit)
                    },
                })
            } else {
                // console.log("dayu")
                wx.showToast({
                    title: '没有更多了',
                })
            }
        } else if (this.data.tpye == "树洞") {
            if ((this.data.current * this.data.limit) < (this.data.total + 10)) {
                wx.hideLoading({
                    success: (res) => {
                        this.getLabelListByType("树洞", this.data.current, this.data.limit)
                    },
                })
            } else {
                // console.log("dayu")
                wx.showToast({
                    title: '没有更多了',
                })
            }
        } else if (this.data.tpye == "失物招领") {
            if ((this.data.current * this.data.limit) < (this.data.total + 10)) {
                wx.hideLoading({
                    success: (res) => {
                        this.getLabelListByType("失物招领", this.data.current, this.data.limit)
                    },
                })
            } else {
                // console.log("dayu")
                wx.showToast({
                    title: '没有更多了',
                })
            }
        }else if (this.data.tpye == "书摘") {
            if ((this.data.current * this.data.limit) < (this.data.total + 10)) {
                wx.hideLoading({
                    success: (res) => {
                        this.getLabelListByType("书摘", this.data.current, this.data.limit)
                    },
                })
            } else {
                // console.log("dayu")
                wx.showToast({
                    title: '没有更多了',
                })
            }
        }
        
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})